package com.decent.ejfadmin.collectcard.service.impl;

import com.decent.ejfadmin.collectcard.dao.CollectCardRecordDao;
import com.decent.ejfadmin.collectcard.entity.SupplyCardViewBean;
import com.decent.ejfadmin.collectcard.response.CollectCardRecordByBatchDto;
import com.decent.ejfadmin.collectcard.response.CollectCardRecordDto;
import com.decent.ejfadmin.collectcard.service.CollectCardDetailService;
import com.decent.ejfadmin.collectcard.service.CollectCardRecordService;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupUser;
import com.decent.ejfadmin.common.bean.SupplyCard;
import com.decent.ejfadmin.common.bean.SupplyProduct;
import com.decent.ejfadmin.common.enums.Consts;
import com.decent.ejfadmin.common.enums.SupplyCardFailReason;
import com.decent.ejfadmin.user.dao.UserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weicx
 * @date 2018/07/17
 */
@Service
@Slf4j
public class CollectCardRecordServiceImpl implements CollectCardRecordService {
    @Autowired
    private CollectCardRecordDao collectCardRecordDao;
    @Autowired
    private CollectCardDetailService collectCardDetailService;
    @Autowired
    private UserDao userDao;

    /**
     * 分页查询收卡记录列表
     *
     * @param pageUnit  分页组件
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param userIds   会员id
     * @param card      销卡bean
     * @return MemberMessage
     */
    @Override
    public Page querySupplyOrderList(Page pageUnit, String startTime, String endTime, String[] userIds,
                                     SupplyCardViewBean card) {
        int total = collectCardRecordDao.apiQuerySupplyOrderListCount(userIds, startTime, endTime, card);
        pageUnit.setTotal(total);
        if (total == 0) {
            pageUnit.setResultList(new ArrayList<>());
        } else {
            List<CollectCardRecordDto> resultList = collectCardRecordDao.apiQuerySupplyOrderListPage(pageUnit, userIds, startTime, endTime, card);
            if (resultList != null && resultList.size() > 0) {
                resultList.forEach((recordDto) -> {
                    SupplyProduct product = null;
                    if (recordDto.getProductId() != null) {
                        product = collectCardRecordDao.queryProductInfoById(recordDto.getProductId());
                    } else {
                        product = collectCardRecordDao.queryProductInfoByFaceAreaType(recordDto.getFace(), recordDto.getArea(), recordDto.getProductType());
                    }
                    if (product != null) {
                        recordDto.setCardOperatorName(product.getCardOperatorName());
                        recordDto.setProductName(product.getProductName());
                    }
                });
            }
            pageUnit.setResultList(resultList);
        }
        return pageUnit;
    }

    /**
     * 修改收卡状态为失败
     *
     * @param batchIds 批次号
     * @param status   状态
     * @return 修改的行数
     */
    @Override
    public int putCardFail(String batchIds, Integer status) {
        List<SupplyCard> supplyCards = collectCardRecordDao.queryTargetCard(batchIds, status);
        int rows = collectCardRecordDao.putCardFail(batchIds, status);
        for (SupplyCard supplyCard : supplyCards) {
            SupUser supUser = userDao.queryUserById(supplyCard.getUserId());
            if (Consts.OPEN.equals(supUser.getApiState())) {
                log.info("放入API异步通知队列的数据[{}]", supplyCard);
                collectCardDetailService.orderAsyn(supplyCard.getOrderId());
            }
        }
        return rows;
    }

    /**
     * 批次查询收卡列表
     *
     * @param page      分页组件
     * @param queryCard 查询bean
     * @return MemberMessage
     */
    @Override
    public Page cardDetailByBatchId(Page page, SupplyCard queryCard) {
        int total = collectCardRecordDao.querySupplyOrderListByBatchCount(queryCard);
        page.setTotal(total);
        if (total == 0) {
            page.setResultList(new ArrayList<>());
        } else {
            List<CollectCardRecordByBatchDto> collectCardRecordByBatchDtos =
                    collectCardRecordDao.querySupplyOrderListByBatchPage(page, queryCard);
            collectCardRecordByBatchDtos.forEach(t ->
                    t.setFailExplian(SupplyCardFailReason.REASON_MAP.get(t.getFailReason())));
            page.setResultList(collectCardRecordByBatchDtos);
        }
        return page;
    }


}
